#########################################
# Unified single-stage environment: Desktop + Android
#########################################
FROM debian:bullseye-slim

WORKDIR /
ARG DEBIAN_FRONTEND=noninteractive
ENV VCPKG_FORCE_SYSTEM_BINARIES=1
ENV VCPKG_ROOT=/vcpkg

# Base tools for both desktop and Android
RUN apt-get update -y && \
    apt-get install --yes --no-install-recommends \
        g++ \
        gcc \
        git \
        curl \
        wget \
        unzip \
        zip \
        python3 \
        python3-pip \
        jq \
        tar \
        ca-certificates \
        build-essential \
        pkg-config \
        clang \
        cmake \
        ninja-build \
        fakeroot \
        rsync \
        nasm \
        yasm \
        libgtk-3-dev \
        libxcb-randr0-dev \
        libxdo-dev \
        libxfixes-dev \
        libxcb-shape0-dev \
        libxcb-xfixes0-dev \
        libasound2-dev \
        libpam0g-dev \
        libpulse-dev \
        libssl-dev \
        sudo \
        libgstreamer1.0-dev \
        libgstreamer-plugins-base1.0-dev \
        openjdk-17-jdk \
    && rm -rf /var/lib/apt/lists/*

# Install CMake (3.30.6) from source to ensure required version
RUN wget https://github.com/Kitware/CMake/releases/download/v3.30.6/cmake-3.30.6.tar.gz --no-check-certificate && \
    tar xzf cmake-3.30.6.tar.gz && \
    cd cmake-3.30.6 && \
    ./configure  --prefix=/usr/local && \
    make && \
    make install

# Note: vcpkg installation is deferred until after Android SDK/NDK is installed

# Create non-root user for dev tasks
RUN groupadd -r user && \
    useradd -r -g user user --home /home/user && \
    mkdir -p /home/user/rustdesk && \
    chown -R user: /home/user && \
    echo "user ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/user

# Desktop runtime dependency (Sciter)
WORKDIR /home/user
RUN curl -LO https://raw.githubusercontent.com/c-smile/sciter-sdk/master/bin.lnx/x64/libsciter-gtk.so

# Rust toolchain for user
USER user
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs > rustup.sh && \
    chmod +x rustup.sh && \
    ./rustup.sh -y

USER root
ENV HOME=/home/user
ENV PATH="$PATH:/home/user/.cargo/bin"

# yq (mikefarah) used by flutter/build_fdroid.sh
RUN wget -O /usr/local/bin/yq https://github.com/mikefarah/yq/releases/download/v4.44.3/yq_linux_amd64 && \
    chmod +x /usr/local/bin/yq

# Android SDK commandline-tools
ENV ANDROID_SDK_ROOT=/opt/android-sdk
ENV ANDROID_HOME=/opt/android-sdk
ENV JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
RUN mkdir -p "$ANDROID_SDK_ROOT/cmdline-tools" && \
    wget -O /tmp/cmdline-tools.zip https://dl.google.com/android/repository/commandlinetools-linux-11076708_latest.zip && \
    unzip -q /tmp/cmdline-tools.zip -d "$ANDROID_SDK_ROOT/cmdline-tools" && \
    mv "$ANDROID_SDK_ROOT/cmdline-tools/cmdline-tools" "$ANDROID_SDK_ROOT/cmdline-tools/latest" && \
    rm /tmp/cmdline-tools.zip && \
    ln -sf /opt/android-sdk/cmdline-tools/latest/bin/sdkmanager /usr/local/bin/sdkmanager

# Accept licenses and install base SDK components
RUN yes | /opt/android-sdk/cmdline-tools/latest/bin/sdkmanager --licenses && \
    /opt/android-sdk/cmdline-tools/latest/bin/sdkmanager "platform-tools" "platforms;android-34" "build-tools;34.0.0" "ndk;25.2.9519653"

ENV ANDROID_NDK_HOME=/opt/android-sdk/ndk/25.2.9519653
ENV ANDROID_ABI=arm64-v8a
ENV ANDROID_PLATFORM=android-21
ENV VCPKG_CHAINLOAD_TOOLCHAIN_FILE="/opt/android-sdk/ndk/25.2.9519653/build/cmake/android.toolchain.cmake"

# vcpkg and common multimedia/libs for desktop + Android build
RUN git clone https://github.com/microsoft/vcpkg /vcpkg && \
    /vcpkg/bootstrap-vcpkg.sh -disableMetrics && \
    /vcpkg/vcpkg --disable-metrics install libvpx libyuv opus aom && \
    /vcpkg/vcpkg --disable-metrics install --triplet arm64-android libvpx libyuv opus aom

# Install Flutter into $HOME (non-root user home)
ENV FLUTTER_HOME=$HOME/flutter
ENV PATH="$FLUTTER_HOME/bin:$ANDROID_SDK_ROOT/cmdline-tools/latest/bin:$JAVA_HOME/bin:$PATH"
RUN git clone https://github.com/flutter/flutter "$FLUTTER_HOME" && \
    cd "$FLUTTER_HOME" && \
    git checkout 3.24.5 && \
    flutter config --no-analytics || true

# Working directory for development
WORKDIR /home/user

COPY ./entrypoint.sh /
# ENTRYPOINT ["/entrypoint.sh"]

# Notes:
# - This single image includes: Desktop toolchain + Android SDK + Flutter + Rust.
# - For Android builds, run scripts like flutter/build_fdroid.sh inside the container.
# - For desktop builds, toolchains (vcpkg, cmake, GTK, etc.) are preinstalled.
